﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using Magnet.Models.NHFluent;

namespace Magnet.Models.NHFluent.Start
{
    public static class Schemas
    {
        public static readonly string NHFluentStart = "NHFStart";
    }

    public class EmployeeMap : ClassMap<Employee>
    {
        public EmployeeMap()
        {
            Schema(Schemas.NHFluentStart);
            Id(x => x.Id);
            Map(x => x.FirstName);
            Map(x => x.LastName);
            References(x => x.Store)
                .Column("StoreId");
            Component(x => x.Address);

            //Component(x => x.Address, m =>
            //    {
            //        m.Map( x => x.AddressLine); 
            //        m.Map( x => x.PostCode); 
            //        m.Map( x => x.City);
            //        m.Map( x => x.State); 
            //        m.Map( x => x.Country); 
            //    }
            //);
        }
    }
    
    public class AddressMap : ComponentMap<Address>
    {
        public AddressMap()
        {
            Map(x => x.AddressLine);
            Map(x => x.PostCode);
            Map(x => x.City);
            Map(x => x.State);
            Map(x => x.Country);
        }
    }

    public class StoreMap : ClassMap<Store>
    {
        public StoreMap()
        {
            Schema(Schemas.NHFluentStart);
            Id(x => x.Id);
            Map(x => x.Name);
            HasMany(x => x.Staff)
              .KeyColumn("StoreId")
              .Inverse()
              .Cascade.All();
            HasManyToMany(x => x.Products)
             .Cascade.All()
             .Schema(Schemas.NHFluentStart)
             .Table("StoreProduct")
             .ParentKeyColumn("StoreId")
             .ChildKeyColumn("ProductId")
             ;
        }
    }

    public class ProductMap : ClassMap<Product>
    {
        public ProductMap()
        {
            Schema(Schemas.NHFluentStart);
            Id(x => x.Id);
            Map(x => x.Name);
            Map(x => x.Price);
            HasManyToMany(x => x.StoresStockedIn)
              .Cascade.All()
              .Inverse()
              .Schema(Schemas.NHFluentStart)
              .Table("StoreProduct")
              .ParentKeyColumn("ProductId")
              .ChildKeyColumn("StoreId");
        }
    }
}
